//
// Created by yanhai on 2021/10/13.
//

#pragma once

#include <string>
#include <array>

namespace muh {
namespace crypto {

// reference Qt5.9.9 qtbase/src/3rdparty/md5/md5.h
typedef unsigned char md5byte;
typedef unsigned int UWORD32;

static_assert(sizeof(UWORD32) == 4, "sizeof(UWORD32) == 4");

struct MD5Context {
    UWORD32 buf[4];
    UWORD32 bytes[2];
    UWORD32 in[16];
};

class Md5 {
    typedef unsigned int value_type;
    typedef std::array<unsigned char, 16> result_type;
public:
    Md5();

    ~Md5() = default;

    // disable copy
    Md5(const Md5 &m) = delete;
    Md5 &operator=(const Md5 &m) = delete;

    void reset();

    void addData(const char *data, value_type length);
    void addData(const std::string &s);

    result_type sum();

    static result_type sum(const char *data, value_type length);
    static result_type sum(const std::string &s);

private:
    MD5Context md5Context;
    result_type mResult;
    bool mHasResult = false;
};

}
}
